home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / New System Software Extensions / QuickDraw™ GX 1.1.2 / Programming Stuff / Sample Code / Graphics Samples / Spectacle ƒ / Main.c next >
Encoding:
C/C++ Source or Header  |  1995-04-10  |  10.9 KB  |  587 lines  |  [TEXT/KAHL]

  1. /*
  2.  *    Main.c
  3.  *
  4.  *    Robert Dierkes,  November 11, 1993
  5.  */
  6.  
  7. /*------------------*/
  8. /*    Include Files    */
  9. /*------------------*/
  10. #include    <GestaltEqu.h>
  11. #include    <QuickDraw.h>
  12. #include    <Types.h>
  13. #include    <Fonts.h>
  14. #include    <Windows.h>
  15. #include    <Memory.h>
  16. #include    <LowMem.h>
  17. #include    <Dialogs.h>
  18.  
  19. #define debugging
  20.  
  21. #include    "graphics toolbox.h"
  22. #include    "graphics libraries.h"
  23. #include    "graphics debugging.h"
  24. #include    "graphics macintosh.h"
  25.  
  26. #include    "Object.h"
  27. #include    "ViewPorts.h"
  28. #include    "Main.h"
  29.  
  30.  
  31. /*----------------------*/
  32. /*    Global Declarations    */
  33. /*----------------------*/
  34. gxGraphicsClient gClient;
  35. Boolean            gQuitApp;
  36. Handle            ghMainMenu;
  37. Rect            gDragRect;
  38. WindowPtr        gWindow;
  39. boolean            gDoAnimation,
  40.                 gCyclePanes;
  41. long            gNextCycle;
  42.  
  43.  
  44. /*------------------------------*/
  45. /*    External Declarations     */
  46. /*------------------------------*/
  47. extern    boolean    gIsSquare;
  48. extern    boolean    gShowFrame;
  49. extern    boolean    gDoDither;
  50. extern    boolean    gDoHalftone;
  51. extern    long    gPanesPerSide;
  52.  
  53.  
  54. long GetNextCycle (long *pNextCycle);
  55.     long
  56. GetNextCycle (long *pNextCycle)
  57. {
  58.     long    now;
  59.  
  60.     if ((now = TickCount ()) > *pNextCycle)
  61.     {
  62.         *pNextCycle = now + kCycleLength;
  63.         return (true);
  64.     }
  65.     else
  66.         return (false);
  67.  
  68. }    /* GetNextCycle */
  69.  
  70.  
  71.     Boolean
  72. InitApp (void)
  73. {
  74.     long            response;
  75.     gxGraphicsError    grfxError;
  76.  
  77.     if (Gestalt (gestaltGraphicsVersion, &response)) {
  78.         DebugStr ("\pQuickDraw GX is not installed");
  79.         return false;
  80.     }
  81.  
  82.     if ((gClient = GXNewGraphicsClient (nil, kGraphicsHeapSize, 0)) == 0) {
  83.         DebugStr ("\pNewGraphicsClient failed");
  84.         return false;
  85.     }
  86.  
  87.     /* Initialize Skia */
  88.     GXEnterGraphics ();
  89.  
  90.     GXSetValidation (gxNoValidation);
  91.     SetGraphicsLibraryErrors ();
  92.     SetGraphicsLibraryNotices();
  93.     InitCommonColors ();
  94.  
  95.     GXIgnoreGraphicsNotice (disposed_dead_caches);
  96.  
  97.     MaxApplZone();
  98.     MoreMasters();
  99.     MoreMasters();
  100.  
  101.     InitGraf (&qd.thePort);
  102.     InitFonts ();
  103.     FlushEvents (everyEvent, 0);
  104.     InitWindows ();
  105.     InitMenus ();
  106.     InitDialogs (nil);
  107.     InitCursor ();
  108.  
  109.     return true;
  110.  
  111. }    /* InitApp */
  112.  
  113.  
  114.     void
  115. ExitApp (void)
  116. {
  117.     if (gWindow)
  118.         DisposeWindow (gWindow);
  119.  
  120.        DisposeCommonColors ();
  121.  
  122.        GXPopGraphicsNotice (/*disposed_dead_caches*/);
  123.  
  124.     GXExitGraphics ();
  125.     if (gClient)
  126.         GXDisposeGraphicsClient (gClient);
  127.  
  128. }    /* ExitApp */
  129.  
  130.  
  131.     void
  132. InitializeMenus (void)
  133. {
  134.     ghMainMenu = GetNewMBar (menuBarRsrcID);
  135.     SetMenuBar (ghMainMenu);
  136.     AddResMenu (GetMHandle (appleMenuRsrcID), 'DRVR');
  137.     DrawMenuBar ();
  138.  
  139.     if (gDoAnimation)    EnableItem  (GetMHandle (windowMenuRsrcID), itemCycle);
  140.     else                DisableItem (GetMHandle (windowMenuRsrcID), itemCycle);
  141.     CheckItem (GetMHandle (objectMenuRsrcID), gIsSquare ? itemSquare : itemOval, true);
  142.     CheckItem (GetMHandle (objectMenuRsrcID), itemDither, gDoDither);
  143.     CheckItem (GetMHandle (objectMenuRsrcID), itemHalftone, gDoHalftone);
  144.     CheckItem (GetMHandle (windowMenuRsrcID), itemShowFrame, gShowFrame);
  145.     CheckItem (GetMHandle (windowMenuRsrcID), itemCycle, gCyclePanes);
  146.     CheckItem (GetMHandle (windowMenuRsrcID), gPanesPerSide - kMinimumPanesPerSide + itemMinimumPanes, true);
  147.  
  148. }    /* InitializeMenus */
  149.  
  150.  
  151.     WindowPtr
  152. InitializeWindow (void)
  153. {
  154.     WindowPtr    pWindow;
  155.     Rect        bounds;
  156.     short        totalMargin;
  157.     Point        newPosition;
  158.     GDHandle    hGDevice;
  159.  
  160.     pWindow = GetNewCWindow (windowRsrcID, nil, kWindowOnTop);
  161.     if (pWindow == nil)
  162.         return (nil);
  163.  
  164.     SetPort (pWindow);
  165.     GXIgnoreGraphicsNotice (transform_already_set);
  166.     SetDefaultViewPort (GXNewWindowViewPort (pWindow));
  167.     GXPopGraphicsNotice ();
  168.  
  169.     /* Find the deepest screen */
  170.     SetRect (&bounds, -32767, -32767, 32767, 32767);
  171.     hGDevice = GetMaxDevice (&bounds);
  172.     bounds = (**hGDevice).gdRect;
  173.  
  174.     totalMargin = (bounds.right - bounds.left) - (pWindow->portRect.right - pWindow->portRect.left);
  175.     newPosition.h = bounds.left + (totalMargin >> 1);
  176.  
  177.     totalMargin = (bounds.bottom - bounds.top) - (pWindow->portRect.bottom - pWindow->portRect.top - kWindowTitleHeight);
  178.     newPosition.v = bounds.top + ((totalMargin + ((hGDevice == GetMainDevice ()) ? GetMBarHeight() : 0)) >> 1);
  179.  
  180.     MoveWindow (pWindow, newPosition.h, newPosition.v, true);
  181.     ShowWindow (pWindow);
  182.  
  183.     gDragRect = qd.screenBits.bounds;
  184.     return (pWindow);
  185.  
  186. }    /* InitializeWindow */
  187.  
  188.  
  189.     void
  190. DoContentClick (EventRecord *pEvent, WindowPtr theWindow)
  191. {
  192.     if (theWindow == nil)
  193.         return;
  194.  
  195.     if (theWindow == FrontWindow ())
  196.     {
  197.         if (GetWindowKind (theWindow) == noGrowDocProc)
  198.         ;
  199.     }
  200.     else
  201.     {
  202.         SetPort (theWindow);
  203.         SelectWindow (theWindow);
  204.     }
  205.  
  206. }    /*    DoContentClick  */
  207.  
  208.  
  209.     void
  210. DoMenuCommand (long menuResult)
  211. {
  212.     short    menuID,
  213.             itemNumber;
  214.  
  215.     if (! menuResult)
  216.         return;
  217.  
  218.     menuID       = menuResult>>16;
  219.     itemNumber = menuResult & 0xFFFF;
  220.  
  221.     switch (menuID)
  222.     {
  223.         case appleMenuRsrcID:
  224.             switch (itemNumber)
  225.             {
  226.                 case itemAbout:
  227.                     Alert (aboutRsrcID, nil);
  228.                     break;
  229.  
  230.                 default:
  231. //                    DoDeskAcc (appleMenuRsrcID, itemNumber);
  232.                     SysBeep (1);
  233.                     break;
  234.             }
  235.             break;
  236.  
  237.         case fileMenuRsrcID:
  238.             switch (itemNumber)
  239.             {
  240.                 case itemQuit:
  241.                     gQuitApp = true;
  242.                     break;
  243.  
  244.                 default:
  245.                     SysBeep (1);
  246.                     break;
  247.             }
  248.             break;
  249.  
  250.         case animationMenuRsrcID:
  251.             switch (itemNumber)
  252.             {
  253.                 case itemBegin:
  254.                     gDoAnimation = true;
  255.                     EnableItem (GetMHandle (windowMenuRsrcID), itemCycle);
  256.                     break;
  257.  
  258.                 case itemEnd:
  259.                     gDoAnimation = false;
  260.                     DisableItem (GetMHandle (windowMenuRsrcID), itemCycle);
  261.                     break;
  262.  
  263.                 case itemStep:
  264.                     MoveObject ();
  265.                     break;
  266.             }
  267.             break;
  268.  
  269.         case objectMenuRsrcID:
  270.             switch (itemNumber)
  271.             {
  272.                 case itemSquare:
  273.                 case itemOval:
  274.                     gIsSquare = (itemNumber == itemSquare);
  275.                     CheckItem (GetMHandle (menuID), itemNumber, true);
  276.                     CheckItem (GetMHandle (menuID), gIsSquare ? itemOval : itemSquare, false);
  277.                     InitializeViewPorts (gWindow);
  278.                     InvalRect (&gWindow->portRect);
  279.                     break;
  280.  
  281.                 case itemDither:
  282.                     CheckItem (GetMHandle (menuID), itemNumber, gDoDither = !gDoDither);
  283.                     ChangeViewPorts (gWindow);
  284.                     InvalRect (&gWindow->portRect);
  285.                     break;
  286.  
  287.                 case itemHalftone:
  288.                     CheckItem (GetMHandle (menuID), itemNumber, gDoHalftone = !gDoHalftone);
  289.                     ChangeViewPorts (gWindow);
  290.                     InvalRect (&gWindow->portRect);
  291.                     break;
  292.             }
  293.             break;
  294.  
  295.         case windowMenuRsrcID:
  296.             if (itemNumber == itemShowFrame)
  297.                 CheckItem (GetMHandle (menuID), itemShowFrame, gShowFrame = ! gShowFrame);
  298.             else if (itemNumber == itemCycle)
  299.             {
  300.                 CheckItem (GetMHandle (menuID), itemCycle, gCyclePanes = ! gCyclePanes);
  301.                 if (gCyclePanes)
  302.                     GetNextCycle (&gNextCycle);
  303.             }
  304.             else
  305.             {
  306.                 long    oldItem;
  307.  
  308.                 CheckItem (GetMHandle (menuID), itemNumber, true);
  309.                 oldItem = gPanesPerSide - kMinimumPanesPerSide + itemMinimumPanes;
  310.                 CheckItem (GetMHandle (menuID), oldItem, false);
  311.                 gPanesPerSide = itemNumber - itemMinimumPanes + kMinimumPanesPerSide;
  312.             }
  313.             ChangeViewPorts (gWindow);
  314.             InvalRect (&gWindow->portRect);
  315.             break;
  316.  
  317.         default:
  318.             break;
  319.     }  /*  switch (menuID)  */
  320.  
  321.     HiliteMenu (kHiliteAllMenus);
  322.  
  323. }    /*    DoMenuCommand  */
  324.  
  325.  
  326.     void
  327. DoGrowBox (EventRecord *pEvent, WindowPtr theWindow)
  328. {
  329.     long        newSize;
  330.     Rect        newRect,
  331.                 oldRect;
  332.  
  333.     oldRect = newRect = theWindow->portRect;
  334.  
  335.     if (newSize = GrowWindow (theWindow, pEvent->where, &qd.screenBits.bounds))
  336.     {
  337.         newRect.right    = newRect.left + (short) newSize;
  338.         newRect.bottom    = newRect.top  + (short) (newSize >> 16);
  339.         SizeWindow (theWindow,
  340.                     newRect.right - newRect.left,
  341.                     newRect.bottom - newRect.top,
  342.                     true);
  343.         InvalRect (&oldRect);
  344.         ChangeViewPorts (gWindow);
  345.     }
  346.  
  347. }    /*    DoGrowBox  */
  348.  
  349.  
  350.     void
  351. DoZoomBox (EventRecord *pEvent, WindowPtr theWindow, short windowPart)
  352. {
  353.     if (TrackBox (theWindow, pEvent->where, windowPart))
  354.     {
  355.         ZoomWindow (theWindow, windowPart, true);
  356.         InvalRect (&theWindow->portRect);
  357.         ChangeViewPorts (gWindow);
  358.     }
  359.  
  360. }    /*    DoZoomBox  */
  361.  
  362.  
  363.     void
  364. DoNullEvent (EventRecord *pEvent)
  365. {
  366.     if (gDoAnimation)
  367.     {
  368.         if (gCyclePanes)
  369.         {
  370.             if (GetNextCycle (&gNextCycle))
  371.             {
  372.                 gPanesPerSide = (gPanesPerSide == kMaximumPanesPerSide)    ? kMinimumPanesPerSide
  373.                                                                         : gPanesPerSide + 1;
  374.                 ChangeViewPorts (gWindow);
  375.                 InvalRect (&gWindow->portRect);
  376.             }
  377.         }
  378.         MoveObject ();
  379.     }
  380.  
  381. }    /*    DoNullEvent  */
  382.  
  383.  
  384.     void
  385. DoMouseDown (EventRecord *pEvent)
  386. {
  387.     short        windowPart;
  388.     WindowPtr    whichWindow;
  389.  
  390.     windowPart = FindWindow (pEvent->where, &whichWindow);
  391.  
  392.     switch (windowPart)
  393.     {
  394.     case inDesk:
  395.         break;
  396.  
  397.     case inMenuBar:
  398.         DoMenuCommand (MenuSelect (pEvent->where));
  399.         break;
  400.  
  401.     case inSysWindow:
  402.         SystemClick (pEvent, whichWindow);
  403.         break;
  404.  
  405.     case inContent:
  406.         DoContentClick (pEvent, whichWindow);
  407.         break;
  408.  
  409.     case inDrag:
  410.         DragWindow (whichWindow, pEvent->where, &gDragRect);
  411.         break;
  412.  
  413.     case inGrow:
  414.         DoGrowBox (pEvent, whichWindow);
  415.         break;
  416.  
  417.     case inGoAway:
  418.           if (TrackGoAway (whichWindow, pEvent->where))
  419.           {
  420.               if (whichWindow == gWindow)
  421.                 gQuitApp = true;
  422.             else
  423.                 DebugStr ("\pDoMouseDown: Clicked in window of unknown close box");
  424.           }
  425.         break;
  426.  
  427.     case inZoomIn:
  428.     case inZoomOut:
  429.         DoZoomBox (pEvent, whichWindow, windowPart);
  430.         break;
  431.  
  432.     default:
  433.         break;
  434. }    /* switch */
  435.  
  436. }    /*    DoMouseDown  */
  437.  
  438.  
  439.     void
  440. DoKeyStroke (EventRecord *pEvent)
  441. {
  442.     char    charCode;
  443.  
  444.     charCode = pEvent->message & charCodeMask;
  445.  
  446.     if (pEvent->modifiers & btnState)
  447.     {
  448.         /*--------------*/
  449.         /* Button is UP */
  450.         /*--------------*/
  451.         if (pEvent->modifiers & cmdKey)
  452.         {
  453.             /*-------------------*/
  454.             /* Command key    x100 */
  455.             /*-------------------*/
  456.             DoMenuCommand (MenuKey (charCode));
  457.         }
  458.     }
  459.  
  460. }    /*    DoKeyStroke  */
  461.  
  462.  
  463.     void
  464. DoUpdate (EventRecord *pEvent)
  465. {
  466.     GrafPtr        savedPort;
  467.     WindowPtr    pUpdateWindow;
  468.  
  469.     GetPort (&savedPort);
  470.  
  471.     pUpdateWindow = (WindowPtr) pEvent->message;
  472.  
  473.     SetPort (pUpdateWindow);
  474.     BeginUpdate (pUpdateWindow);
  475.  
  476.     EraseRgn (pUpdateWindow->visRgn);
  477.  
  478.     if (pUpdateWindow == gWindow)
  479.     {
  480.         if (gShowFrame)
  481.             DrawObjectFrame ();
  482.         DrawObject ();
  483.     }
  484.  
  485.     EndUpdate (pUpdateWindow);
  486.     SetPort (savedPort);
  487.  
  488. }    /*    DoUpdate  */
  489.  
  490.  
  491.     void
  492. DoActivate (EventRecord    *pEvent)
  493. {
  494.     WindowPtr    pActiveWindow;
  495.  
  496.     /*--------------------------------------*/
  497.     /* Get the window to de/activate        */
  498.     /* and its kind from the event message    */
  499.     /*--------------------------------------*/
  500.     pActiveWindow = (WindowPtr) pEvent->message;
  501.  
  502.     SetPort (pActiveWindow);
  503.  
  504.     /*-----------------*/
  505.     /* Activate window */
  506.     /*-----------------*/
  507.     if (pEvent->modifiers & activeFlag)
  508.     {
  509.         /* Enable/Disable items */
  510.     }
  511.     else
  512.     /*-------------------*/
  513.     /* Deactivate window */
  514.     /*-------------------*/
  515.     {
  516.         /* Enable/Disable items */
  517.     }
  518.  
  519. }    /*    DoActivate  */
  520.  
  521.  
  522.     void
  523. DoEvent  (void)
  524. {
  525.     EventRecord    theEvent;
  526.  
  527.     SystemTask ();        /* Handle desk accessories */
  528.  
  529.     GetNextEvent (everyEvent, &theEvent);
  530.  
  531.     switch (theEvent.what)
  532.     {
  533.     case nullEvent:
  534.         DoNullEvent (&theEvent);
  535.         break;
  536.  
  537.     case mouseDown:
  538.         DoMouseDown (&theEvent);
  539.         break;
  540.  
  541.     case keyDown:
  542.     case autoKey:
  543.         DoKeyStroke (&theEvent);
  544.         break;
  545.  
  546.     case updateEvt:
  547.         DoUpdate (&theEvent);
  548.         break;
  549.  
  550.     case activateEvt:
  551.         DoActivate (&theEvent);
  552.         break;
  553.  
  554.     case mouseUp:
  555.     case keyUp:
  556.     case diskEvt:
  557.     case networkEvt:
  558.     case driverEvt:
  559.     default:
  560.         break;
  561.     }  /* switch (theEvent.what)  */
  562.  
  563. }    /* DoEvent */
  564.  
  565.  
  566. main (void)
  567. {
  568.     gQuitApp = false;
  569.     if (InitApp ())
  570.     {
  571.         InitializeViewPortGlobals ();
  572.  
  573.         InitializeMenus ();
  574.         if (gWindow = InitializeWindow ())
  575.         {
  576.             InitializeViewPorts (gWindow);
  577.  
  578.             while (! gQuitApp)
  579.                 DoEvent ();
  580.  
  581.             DisposeViewPorts (gWindow);
  582.         }
  583.         ExitApp ();
  584.     }
  585.  
  586. }    /* main */
  587.